﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MultiLockBWDoor : MonoBehaviour {
	
	public Renderer[] barRends;
	
	public Texture[] tex1;
	public Texture[] tex2;
	
	public List<MBDButtonScript> myButtons;
	int buttonAmountCounter = 0;
	List<bool> activeButs;
	public bool activated = false;
	float timeCounter = 0;
	public Transform stepOn;
	
	public void addAButton(MBDButtonScript button)
	{
		if(myButtons == null)
			myButtons = new List<MBDButtonScript>();
		if(activeButs == null)
			activeButs = new List<bool>();
		
		activeButs.Add(false);
		myButtons.Add(button);
		barRends[buttonAmountCounter].gameObject.SetActive(true);
		button.buttonOfDoorID = buttonAmountCounter;
		
		buttonAmountCounter++;
	}
	
	public void setButtonActive(int ID, bool activeNess)
	{
		Texture[] tmpTex;
		if(ID % 2 == 0)
			tmpTex = tex1;
		else
			tmpTex = tex2;
		
		int tmpID;
		
		if(activeNess)
			tmpID = 1;
		else
			tmpID = 0;
		
		activeButs[ID] = activeNess;
		barRends[ID].material.mainTexture = tmpTex[tmpID];
		
		bool allDone = true;
		for(int i = 0 ; i < activeButs.Count; i++)
			if(!activeButs[i])
				allDone = false;
		
		if(allDone)
		{
			for(int i = 0; i < myButtons.Count; i++)
			{
				//Debug.Log("KILL");
				myButtons[i].killFunctionality();
				
				if(i % 2 == 0)
					tmpTex = tex1;
				else
					tmpTex = tex2;
				
				barRends[ID].material.mainTexture = tmpTex[1];
			}
			
			activated = true;
		}
	
	}
	
	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
		if(activated)
		{
			for(int i = 0; i < buttonAmountCounter;i++)
			{
				Color tmpCol = barRends[i].material.color;
				tmpCol.a -= Time.deltaTime * 1.5f;
				if(tmpCol.a <= 0)
				{
					collider.isTrigger = true;
					tmpCol.a = 0;
					activated = false;
					barRends[i].enabled = false;
					barRends[i].material.color = tmpCol;
					if(stepOn != null)
						Destroy(stepOn.gameObject);
				}
				barRends[i].material.color = tmpCol;
			}
		}
	}
}
